package com.card.bleNfcBase;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.lelaohui.net.BaseLeLaohuiAcitivity;
import com.server.llh.llhserverwork.bean.Constants;

/**
 * Created by ZTF on 2017-09-12.
 */

public abstract class LLHBleNfcBaseActivity extends BaseLeLaohuiAcitivity {

    public LocalBroadcastManager getLocalBroadcastManager() {
        return localBroadcastManager;
    }

    public void setLocalBroadcastManager(LocalBroadcastManager localBroadcastManager) {
        this.localBroadcastManager = localBroadcastManager;
    }

    private LocalBroadcastManager localBroadcastManager = null;
    MyBroadActivity myBroadActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        localBroadcastManager=LocalBroadcastManager.getInstance(getApplicationContext());
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constants.BroadCastAction.READ_CARD_NO);
        myBroadActivity = new MyBroadActivity();
        this.registerReceiver(myBroadActivity, intentFilter);
    }

    private class MyBroadActivity extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intentdata) {
            String action = intentdata.getAction();
            Log.d("cardId","action=="+action);
            if (Constants.BroadCastAction.READ_CARD_NO.equals(action)) {
                String cardId = intentdata.getStringExtra("cardNo");
                Log.d("cardId","cardId22=="+cardId);
                intentdata.putExtra("packname", "com.lelaohui.activity");
                intentdata.setAction("com.lelaohui.llhreadcardlib.getCardId");
                intentdata.putExtra("cardId", cardId + "");
                localBroadcastManager.sendBroadcast(intentdata);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myBroadActivity!=null){
            unregisterReceiver(myBroadActivity);
        }
    }
}
